package com.zengguang.testkaoshi.services.impl;

import com.zengguang.testkaoshi.dao.RolePersonAuthDao;
import com.zengguang.testkaoshi.pojo.Rolepersonauth;
import com.zengguang.testkaoshi.services.IRolePersonAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class RolePersonAuthService implements IRolePersonAuthService {
    @Autowired
    private RolePersonAuthDao rolePersonAuthDao;
    //查询全部
    @Override
    public List<Rolepersonauth> selectAllRolepersonauth() {
        List<Rolepersonauth> list=rolePersonAuthDao.selectAllRolepersonauth();
        return list;
    }
    //增加权限
    @Override
    public int insertRolepersonauth(Rolepersonauth rolepersonauth) {
        int temp = 0;
        /* 业务判断
         * （1）权限名称不能重复
         * （2）增加权限之前先查询*/
        try{
            temp = rolePersonAuthDao.selectRolepersonauthById(rolepersonauth);
            if (temp < 1) {
                temp=rolePersonAuthDao.insertRolepersonauth(rolepersonauth);}
        } catch (Exception ex) {
            temp = -1;
            System.out.println("插入权限信息执行SQL报错："+ex.getMessage());
        }
        return temp;
    }
    //删除权限
    @Override
    public int deleteRolepersonauth(Rolepersonauth rolepersonauth) {
        int temp = 0;
        /*1.有关业务流程的判断*/
        //(1)删除有关业务
        try {
            temp = rolePersonAuthDao.selectRolepersonauthById(rolepersonauth);
            if (temp > 0) {
                rolePersonAuthDao.deleteRolepersonauth(rolepersonauth);
            }
        } catch (Exception ex) {
            temp = -1;
            System.out.println("删除权限信息执行SQL报错："+ex.getMessage());
        }

        return temp;
    }
    //权限信息更新
    @Override
    public int updateRolepersonauth(Rolepersonauth rolepersonauth) {
        int temp = 0;
        try {
            temp = rolePersonAuthDao.selectRolepersonauthByIdCount(rolepersonauth.getRolepersonauthid());
            if (temp > 0) {
                temp=rolePersonAuthDao.updateRolepersonauth(rolepersonauth);
            }
        } catch (Exception ex) {
            temp = -1;
            //打印错误信息
            System.out.println("权限更新信息执行SQL报错："+ex.getMessage());
        }

        return temp;
    }
}
